package org.openjsonp.json;

import java.math.BigDecimal;
import java.math.BigInteger;

import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;

/**
 * Implementation of interface {@link javax.json.JsonArrayBuilder
 * JsonArrayBuilder}
 * 
 */
public class JsonArrayBuilderImpl implements JsonArrayBuilder {

	private JsonArray jsonArray;

	/**
	 * Default constructor.
	 */
	public JsonArrayBuilderImpl() {
		jsonArray = new JsonArrayImpl();
	}

	@Override
	public JsonArrayBuilder add(JsonValue value) {
		if (value == null) {
			throw new NullPointerException("The specified javax.json.JsonValue is null and it is not permit here.");
		}

		jsonArray.add(value);
		return this;
	}

	@Override
	public JsonArrayBuilder add(String value) {
		if (value == null) {
			throw new NullPointerException("The specified String value is null and it is not permit here.");
		}

		jsonArray.add(new JsonStringImpl(value));
		return this;
	}

	@Override
	public JsonArrayBuilder add(BigDecimal value) {
		if (value == null) {
			throw new NullPointerException("The specified BigDecimal value is null and it is not permit here.");
		}

		// TODO: Best approch ?
		jsonArray.add(new JsonNumberImpl(value.toString()));
		return this;
	}

	@Override
	public JsonArrayBuilder add(BigInteger value) {
		if (value == null) {
			throw new NullPointerException("The specified BigInteger value is null and it is not permit here.");
		}
		jsonArray.add(new JsonNumberImpl(value));
		return this;
	}

	@Override
	public JsonArrayBuilder add(int value) {
		jsonArray.add(new JsonNumberImpl(value));
		return this;
	}

	@Override
	public JsonArrayBuilder add(long value) {
		jsonArray.add(new JsonNumberImpl(value));
		return this;
	}

	@Override
	public JsonArrayBuilder add(double value) {
		jsonArray.add(new JsonNumberImpl(value));
		return this;
	}

	@Override
	public JsonArrayBuilder add(boolean value) {
		if (value) {
			jsonArray.add(JsonValue.TRUE);
		}
		else {
			jsonArray.add(JsonValue.FALSE);
		}
		return this;
	}

	@Override
	public JsonArrayBuilder addNull() {
		jsonArray.add(JsonValue.NULL);
		return this;
	}

	@Override
	public JsonArrayBuilder add(JsonObjectBuilder builder) {
		if (builder == null) {
			throw new NullPointerException("The specified JsonObjectBuilder builder is null and it is not permit here.");
		}
		jsonArray.add(builder.build());
		return this;
	}

	@Override
	public JsonArrayBuilder add(JsonArrayBuilder builder) {
		if (builder == null) {
			throw new NullPointerException("The specified JsonArrayBuilder builder is null and it is not permit here.");
		}
		jsonArray.add(builder.build());
		return this;
	}

	@Override
	public JsonArray build() {
		return jsonArray;
	}

}
